package com.futures.investm.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.futures.investm.R;
import com.aries.ui.view.title.TitleBarView;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.Arrays;

import tech.com.commoncore.adapter.ViewPagerAdapter;
import tech.com.commoncore.base.BaseTitleFragment;

public class MarketFragment extends BaseTitleFragment {

    private final String[] TABS = {"国内","原油","贵金属"};
    private SegmentTabLayout mTabLayout;
    private ViewPager mViewPager;

    private ViewPagerAdapter mAdapter;

    public static MarketFragment newInstance() {
        Bundle bundle = new Bundle();
        MarketFragment fragment = new MarketFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void setTitleBar(TitleBarView titleBar) {
        //设置中间Segment
        View view = View.inflate(mContext, R.layout.layout_market_tab, null);
        mTabLayout = view.findViewById(R.id.tabLayout);
        titleBar.addCenterAction(titleBar.new ViewAction(view));
        titleBar.setStatusBarLightMode(false)
                .setLeftTextDrawable(null);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_market;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mViewPager = mContentView.findViewById(R.id.vp_contentFastLib);

        ArrayList<Fragment> fragments = new ArrayList<>();
//        fragments.add(MarketToMarketFragment.newInstance());
        fragments.add(ChinaMarketFragment.newInstance());
        fragments.add(OilMarketFragment.newInstance());
        fragments.add(MetalMarketFragment.newInstance());

        mTabLayout.setTabData(TABS);
        mAdapter = new ViewPagerAdapter(getChildFragmentManager(), fragments, Arrays.asList(TABS));
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(2);

        initListener();

        mViewPager.setCurrentItem(0);
    }

    private void initListener() {
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
